/**
 * 
 */
package calculator;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.KeyStroke;

/**
 * @author davide
 *
 */
public class MenuBuilder
{
    public static JMenu createMenuEntry(String text, int shortcut)
    {
	JMenu menu = new JMenu(text);
	
	menu.setMnemonic(shortcut);
	return menu;
    }
    
    public static JMenu createMenuEntry(String text)
    {
	return new JMenu(text);
    }
    
    public static JMenuItem createMenuSubEntry(String text, ActionListener action)
    {
	JMenuItem item = new JMenuItem(text);
	
	item.addActionListener(action);
	return item;
    }
    
    public static JMenuItem createMenuSubEntry(String text, ImageIcon icon, ActionListener action)
    {
	JMenuItem item = new JMenuItem(text, icon);
	
	item.addActionListener(action);
	return item;
    }
    
    public static JMenuItem createMenuSubEntry(String text, ImageIcon icon, int keyCode, ActionListener action)
    {
	JMenuItem item = new JMenuItem(text, icon);
	
	item.setAccelerator(KeyStroke.getKeyStroke(keyCode, ActionEvent.CTRL_MASK));
	item.addActionListener(action);	
	return item;
    }
    
    public static JMenuItem createMenuSubEntry(String text, String keySequence, ActionListener action)
    {
	JMenuItem item = new JMenuItem(text);
	
	item.setAccelerator(KeyStroke.getKeyStroke(keySequence));
	item.addActionListener(action);
	return item;
    }
    
    public static JMenuItem createMenuSubEntry(String text, ImageIcon icon, String keySequence, ActionListener action)
    {
	JMenuItem item = new JMenuItem(text, icon);
	
	item.setAccelerator(KeyStroke.getKeyStroke(keySequence));
	item.addActionListener(action);
	return item;
    }
    
    public static JRadioButtonMenuItem createMenuRadioEntry(String text, ImageIcon icon, int keyCode, ButtonGroup group, boolean selected, ActionListener action)
    {
	JRadioButtonMenuItem item = new JRadioButtonMenuItem(text, icon);
	
	item.setSelected(selected);
	item.setMnemonic(keyCode);
	item.setAccelerator(KeyStroke.getKeyStroke(keyCode, ActionEvent.CTRL_MASK));
	group.add(item);
	item.addActionListener(action);
	
	return item;	
    }
    
    public static JRadioButtonMenuItem createMenuRadioEntry(String text, ImageIcon icon, ButtonGroup group, boolean selected, ActionListener action)
    {
	JRadioButtonMenuItem item = new JRadioButtonMenuItem(text, icon);
	
	item.setSelected(selected);
	group.add(item);
	item.addActionListener(action);
	
	return item;	
    }
}
